package com.schooleducation.service.impl;

import java.util.Date;
import java.util.List;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.LessonContentInfo;
import com.schooleducation.entity.LessonInfo;
import com.schooleducation.entity.LessonOneType;
import com.schooleducation.entity.LessonSubscribe;
import com.schooleducation.entity.LessonTwoType;
import com.schooleducation.entity.User;
import com.schooleducation.service.LessonService;

public class LessonServiceImpl extends SuperService implements LessonService {

	@Override
	public List<LessonOneType> getLessonTypeAll() {
		// TODO Auto-generated method stub
		List<LessonOneType> list = super.getDaoFac().getLessonOneTypeDao().getOneAndTwoType(0, 0);

		return list;
	}

	@Override
	public Long getSubscribeLessonCount() {
		// TODO Auto-generated method stub

		return super.getDaoFac().getLessonSubscribeDao().getTotalCount(LessonSubscribe.class);
	}

	@Override
	public List<LessonSubscribe> getSubscribeLesson(User user, int page) {
		// TODO Auto-generated method stub
		LessonSubscribe lessonSubscribe = new LessonSubscribe();
		lessonSubscribe.setUser(user);

		return super.getDaoFac().getLessonSubscribeDao().getSubscribeLessonByUser(lessonSubscribe, page, 20);
	}

	@Override
	public List<Lesson> getLessonList(int page) {
		// TODO Auto-generated method stub

		return super.getDaoFac().getLessonDao().findByPage(Lesson.class, page, 20);
	}

	@Override
	public boolean addLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// 数据合法性验证
		if (lesson.getTitle().length() >= 2) {
			lesson.setCreateDate(new Date());
			// 保存课程基本信息
			super.getDaoFac().getLessonDao().save(lesson);
			LessonInfo li = lesson.getLessonInfo();
			li.setLesson(lesson);
			// 保存课程详细信息
			super.getDaoFac().getLessonInfoDao().save(li);
			flag = true;
		}

		return flag;
	}

	@Override
	public List<Lesson> getLessonListByTeacher(User teacher) {
		// TODO Auto-generated method stub
		Lesson lesson = new Lesson();
		lesson.setTeacher(teacher);

		return super.getDaoFac().getLessonDao().getLessonListByTeacher(lesson, 0, 0);
	}

	@Override
	public boolean addLessonContent(LessonContent lc) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// 数据合法验证
		if (lc.getTitle().length() >= 0 && lc.getContentInfo().getInfo().length() > 15) {
			lc.setCreateDate(new Date());
			LessonContentInfo lci = lc.getContentInfo();
			lci.setLessonContent(lc);
			// 存储课程基本内容
			super.getDaoFac().getLessonContentDao().save(lc);
			// 存储课程内容的文档
			super.getDaoFac().getLessonContentInfoDao().save(lci);
			flag = true;
		}

		return flag;
	}

	@Override
	public boolean addSubscribeLesson(LessonSubscribe ls) {
		// TODO Auto-generated method stub
		boolean flag = false;

		// 查询是否提交了订阅信息
		if (super.getDaoFac().getLessonSubscribeDao().getCountByLessonAndUser(ls) == 0) {
			ls.setCreateDate(new Date());
			// 订阅课程默认为待审核阶段
			ls.setState(1);
			super.getDaoFac().getLessonSubscribeDao().save(ls);
			flag = true;
		}

		return flag;
	}

	@Override
	public List<LessonContent> getLessonContentList(int lessonId) {
		// TODO Auto-generated method stub
		LessonContent lc = new LessonContent();
		Lesson lesson = new Lesson();
		lesson.setId(lessonId);
		lc.setLesson(lesson);

		return super.getDaoFac().getLessonContentDao().getLessonContentListByLesson(lc, lessonId, 0);
	}

	@Override
	public LessonContentInfo getLessonContentInfo(int lessonContentId) {
		// TODO Auto-generated method stub

		return super.getDaoFac().getLessonContentInfoDao().findById(LessonContentInfo.class, lessonContentId);
	}

	@Override
	public Lesson getLessonById(int id) {
		// TODO Auto-generated method stub
		Lesson l = new Lesson();
		l.setId(id);
		
		l = super.getDaoFac().getLessonDao().getLessonById(l);
		
		return l;
	}

	@Override
	public List<Lesson> getLessonListByType(Lesson lesson, int page) {
		// TODO Auto-generated method stub

		return super.getDaoFac().getLessonDao().getLessonListByType(lesson, page, 20);
	}

	@Override
	public int getPageByTotalNum(int num, int limit) {
		// TODO Auto-generated method stub

		return super.getPageCount(num, limit);
	}

	@Override
	public LessonContent getLessonContentById(int contentId) {
		// TODO Auto-generated method stub
		return super.getDaoFac().getLessonContentDao().findById(LessonContent.class, contentId);
	}

	@Override
	public boolean updateLessonContent(LessonContent lc) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// 数据合法性验证
		if (lc.getTitle().length() >= 0 && lc.getContentInfo().getInfo().length() > 15) {
			lc.setCreateDate(new Date());
			LessonContentInfo lci = lc.getContentInfo();
			lci.setId(lc.getId());
			// 更新课题基本信息
			super.getDaoFac().getLessonContentDao().update(lc);
			// 更新课题详细信息
			super.getDaoFac().getLessonContentInfoDao().update(lci);
			flag = true;
		}

		return flag;
	}

	@Override
	public boolean deleteLessonContent(int lessonContentId) {
		// TODO Auto-generated method stub
		super.getDaoFac().getLessonContentDao().deleteById(LessonContent.class, lessonContentId);
		super.getDaoFac().getLessonContentInfoDao().deleteById(LessonContentInfo.class, lessonContentId);

		return true;
	}

	@Override
	public boolean deleteSubscribeLesson(int subscribeId) {
		// TODO Auto-generated method stub
		super.getDaoFac().getLessonSubscribeDao().deleteById(LessonSubscribe.class, subscribeId);

		return true;
	}

	@Override
	public boolean getIsSubscribeLessonPass(LessonSubscribe ls) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// 查询订阅课程是否通过
		ls.setState(2);
		if (super.getDaoFac().getLessonSubscribeDao().getCountStateByLessonAndUser(ls) > 0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public LessonInfo getLessonInfoById(int lessonId) {
		// TODO Auto-generated method stub

		return super.getDaoFac().getLessonInfoDao().findById(LessonInfo.class, lessonId);
	}

	@Override
	public boolean updateLesson(Lesson lesson) {
		// TODO Auto-generated method stub
		boolean flag = false;
		// 数据合法性验证
		if (lesson.getTitle().length() >= 2) {
			lesson.setCreateDate(new Date());
			// 更新课程基本信息
			super.getDaoFac().getLessonDao().update(lesson);
			LessonInfo li = lesson.getLessonInfo();
			li.setId(lesson.getId());
			// 更新课程详细信息
			super.getDaoFac().getLessonInfoDao().update(li);
			flag = true;
		}

		return flag;
	}

	@Override
	public List<LessonOneType> getLessonOneTypeList() {
		// TODO Auto-generated method stub
		return super.getDaoFac().getLessonOneTypeDao().findAll(LessonOneType.class);
	}

	@Override
	public List<LessonTwoType> getLessonTwoTypeListByOneType(int oneTypeId) {
		// TODO Auto-generated method stub
		LessonOneType lot = new LessonOneType();
		lot.setId(oneTypeId);
		LessonTwoType ltt = new LessonTwoType();
		ltt.setOneType(lot);

		return super.getDaoFac().getLessonTwoTypeDao().getTwoTypeListByOneType(ltt, 0, 0);
	}

	@Override
	public boolean addLessonOneType(LessonOneType lot) {
		// TODO Auto-generated method stub
		super.getDaoFac().getLessonOneTypeDao().save(lot);

		return true;
	}

	@Override
	public boolean updateLessonOneType(LessonOneType lot) {
		// TODO Auto-generated method stub
		boolean flag = false;
		LessonOneType lotype = super.getDaoFac().getLessonOneTypeDao().findById(LessonOneType.class, lot.getId());
		if (lot != null) {
			lotype.setTypeName(lot.getTypeName());
			flag = true;
		}

		return flag;
	}

	@Override
	public boolean deleteLessonOneType(int oneTypeId) {
		// TODO Auto-generated method stub
		LessonTwoType ltt = new LessonTwoType();
		LessonOneType lot = new LessonOneType();
		lot.setId(oneTypeId);
		ltt.setOneType(lot);
		//删除二级课程类
		super.getDaoFac().getLessonTwoTypeDao().deleteTwoTypeByOneType(ltt);
		//删除一级课程类
		super.getDaoFac().getLessonOneTypeDao().deleteById(LessonOneType.class, oneTypeId);

		return true;
	}

	@Override
	public boolean addLessonTwoType(LessonTwoType ltt) {
		// TODO Auto-generated method stub
		super.getDaoFac().getLessonTwoTypeDao().save(ltt);

		return true;
	}

	@Override
	public boolean updateLessonTwoType(LessonTwoType ltt) {
		// TODO Auto-generated method stub
		boolean flag = false;
		LessonTwoType lttype = super.getDaoFac().getLessonTwoTypeDao().findById(LessonTwoType.class, ltt.getId());
		if (lttype != null) {
			lttype.setTypeName(ltt.getTypeName());
			flag = true;
		}

		return flag;
	}

	@Override
	public boolean deleteLessonTwoType(int twoTypeId) {
		// TODO Auto-generated method stub

		super.getDaoFac().getLessonTwoTypeDao().deleteById(LessonTwoType.class, twoTypeId);

		return true;
	}

	@Override
	public List<Lesson> gethotLessonList(int start, int limit) {
		// TODO Auto-generated method stub
		List<Lesson> ls = super.getDaoFac().getLessonDao().getLessonListByCount(start, limit);
		
		return ls;
	}

	@Override
	public List<Lesson> getNewLessonList(int start, int limit) {
		// TODO Auto-generated method stub
		List<Lesson> ls = super.getDaoFac().getLessonDao().getLessonListDescId(start, limit);
		
		return ls;
	}

	@Override
	public List<LessonContent> getHotLessonContentList(int start, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<LessonContent> getNewLessonContentList(int start, int limit) {
		// TODO Auto-generated method stub
		List<LessonContent> lc = super.getDaoFac().getLessonContentDao().getLessonContentListDescId(start, limit);
		
		return lc;
	}

	@Override
	public LessonSubscribe getSubscribeLessonByLessonAndUser(LessonSubscribe ls) {
		// TODO Auto-generated method stub
		ls = super.getDaoFac().getLessonSubscribeDao().getSubscribeByLessonAndUser(ls);
		
		return ls;
	}

}
